package com.yxx.business.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yxx.business.entity.*;
import com.yxx.business.service.ITbProjectEngineeringService;
import com.yxx.business.service.ITbProjectInfoService;
import com.yxx.common.annotation.DataScope;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbEngineeringMapper;
import com.yxx.business.service.ITbEngineeringService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工程信息Service业务层处理
 *
 * @author yxx
 * @date 2025-08-03
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbEngineeringServiceImpl extends ServiceImplPlus<TbEngineeringMapper, TbEngineeringDo> implements ITbEngineeringService {

    private final ITbEngineeringService self;

    private final ITbProjectInfoService iTbProjectInfoService;

    private final TbEngineeringMapper tbEngineeringMapper;

    private final ITbProjectEngineeringService iTbProjectEngineeringService;

    /**
     * 查询工程信息分页结果
     *
     * @param tbEngineeringQueryDto 工程信息查询实体
     * @return 工程信息分页
     */
    @Override
    public PageResult<TbEngineeringVo> selectTbEngineeringPage(TbEngineeringQueryDto tbEngineeringQueryDto) {
        startPage();
        PageResult<TbEngineeringVo> page = new PageResult<>();
        List<TbEngineeringVo> tbEngineeringVos = self.selectTbEngineeringVoList(tbEngineeringQueryDto);
        PageInfo<TbEngineeringVo> pageInfo = new PageInfo<>(tbEngineeringVos);
        BeanUtil.copyProperties(pageInfo, page);
        page.setRows(tbEngineeringVos);
        clearPage();
        return page;
    }

    /**
     * 查询工程信息Vo列表
     *
     * @param tbEngineeringQueryDto 工程信息查询实体
     * @return 工程信息集合
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TbEngineeringVo> selectTbEngineeringVoList(TbEngineeringQueryDto tbEngineeringQueryDto) {
        return tbEngineeringMapper.selectTbEngineeringList(tbEngineeringQueryDto);
    }

    /**
     * 查询单个工程信息
     *
     * @param engineeringId 工程信息主键
     * @return 工程信息单个
     */
    @Override
    public TbEngineeringVo selectTbEngineeringByEngineeringId(Long engineeringId) {
        TbEngineeringVo tbEngineeringVo = super.convertBean(tbEngineeringMapper.selectTbEngineeringByEngineeringId(engineeringId), TbEngineeringVo.class);
        TbProjectEngineeringDo one = iTbProjectEngineeringService.getOne(new LambdaQueryWrapper<TbProjectEngineeringDo>()
                .eq(TbProjectEngineeringDo::getEngineeringId, engineeringId)
        );
        if (one != null) {
            TbProjectInfoVo tbProjectInfoVo = iTbProjectInfoService.selectTbProjectInfoVoByProjectId(one.getProjectId());
            tbEngineeringVo.setProjectName(tbProjectInfoVo.getProjectName());
            tbEngineeringVo.setProjectId(tbProjectInfoVo.getProjectId());
        }
        return tbEngineeringVo;
    }

    /**
     * 新增工程信息
     *
     * @param tbEngineeringEditDto 工程信息编辑实体
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTbEngineering(TbEngineeringEditDto tbEngineeringEditDto) {
        tbEngineeringEditDto.setCreateBy(getUserNameOrNotLogged());
        tbEngineeringEditDto.setCreateTime(DateUtils.getNowDate());
        TbEngineeringDo tbEngineeringDo = super.convertT(tbEngineeringEditDto).setCreateById(getUserIdOrNotLogged());
        int i = tbEngineeringMapper.insertTbEngineering(tbEngineeringDo);
        iTbProjectEngineeringService.insertTbProjectEngineering(
                new TbProjectEngineeringEditDto()
                        .setEngineeringId(tbEngineeringDo.getEngineeringId())
                        .setProjectId(tbEngineeringEditDto.getProjectId())
        );
        return i;
    }

    /**
     * 修改工程信息
     *
     * @param engineeringId 工程信息主键
     * @param tbEngineeringQueryDto 工程信息编辑实体
     * @return 结果
     */
    @Override
    public int updateTbEngineering(Long engineeringId, TbEngineeringEditDto tbEngineeringQueryDto) {
        tbEngineeringQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbEngineeringQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbEngineeringMapper.updateTbEngineering(super.convertT(tbEngineeringQueryDto).setEngineeringId(engineeringId));
    }

    /**
     * 批量删除工程信息
     *
     * @param engineeringIds 需要删除的工程信息主键集合
     * @return 结果
     */
    @Override
    public int deleteTbEngineeringByEngineeringIds(List<Long> engineeringIds) {
        engineeringIds.forEach(self::deleteTbEngineeringByEngineeringId);
        return engineeringIds.size();
    }

    /**
     * 删除单个工程信息信息
     *
     * @param engineeringId 工程信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTbEngineeringByEngineeringId(Long engineeringId) {
        int i = tbEngineeringMapper.deleteTbEngineeringByEngineeringId(engineeringId);
        iTbProjectEngineeringService.remove(new LambdaQueryWrapper<TbProjectEngineeringDo>()
                .eq(TbProjectEngineeringDo::getEngineeringId, engineeringId)
        );
        return i;
    }
}
